package com.whately.core.base.jwt;

import com.whately.core.base.constant.ConstantProject;
import com.whately.core.base.constant.DebugSwitcher;
import com.whately.core.base.constant.LoginUser;
import com.whately.core.base.jwt.util.JWTUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

class FetchLoginUserHelper {

    public static LoginUser getLoginUserWithDebugBuild(HttpServletRequest request){
        if(request==null){
            return null;
        }
        String token = request.getHeader(ConstantProject.response_header_token_name);// 从 http 请求头中取出 token

        if(StringUtils.isNotBlank(token)){
            LoginUser loginUser = JWTUtil.fetchJwtUserFromToken(token);
            return loginUser;
        }else {
            /// debug 模式 ，生成一个假的loginInfo
            if (DebugSwitcher.isDebugOpen(request)) {
                LoginUser loginUser = new LoginUser();
                loginUser.setUserId(request.getParameter("debugId") == null ? "2" : request.getParameter("debugId"));
                loginUser.setUserType(request.getParameter("debugRole")==null?1:Integer.parseInt(request.getParameter("debugRole")));
                return loginUser;
            }
        }

        return null;
    }
}
